package com.panshi.controller;

import com.panshi.entity.AppCommentT;
import com.panshi.entity.AppSearchT;
import com.panshi.entity.AppUserT;
import com.panshi.result.R;
import com.panshi.service.CommentClientService;
import com.panshi.service.UserClientService;
import com.panshi.service.ZxClientService;
import com.panshi.util.UserInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentClientService commentClientService;

    @Autowired
    private ZxClientService zxClientService;

    @Autowired
    private UserInfoUtils userInfoUtils;

    @Autowired
    private UserClientService userClientService;

    //搜索
    @GetMapping("/search")
    public String search(HttpServletRequest request, Model model, HttpServletResponse response){
        String sear = request.getParameter("sear");
        AppUserT getuser = userInfoUtils.getuser(request, response);

        //搜索
        model.addAttribute("sear",sear);

        //用户信息
        model.addAttribute("uservo",getuser);

        //搜索前十条sentence1.replace("中学生", sentence2);
        List<AppSearchT> appSearchTS = commentClientService.searchPage(1, 10, sear);
        if(appSearchTS!=null){
            appSearchTS.forEach(item->{
                item.setClassifyT(zxClientService.bycid(item.getClassifyId()));
                item.setAppUserT(userClientService.byid(item.getUserId()));
               item.setSearchTitle(item.getSearchTitle().replace(sear,"<span style=\"color:red\">"+sear+"</span>"));
            });
        }
        model.addAttribute("appSearchTS",appSearchTS);

        //十条热搜索
        List<AppSearchT> appSearchTS1 = commentClientService.searchNew(1, 10);
        Collections.shuffle(appSearchTS1);
        model.addAttribute("readSearch",appSearchTS1);

        //总数
        Integer searchCount = commentClientService.searchCount(sear);
        model.addAttribute("searchCount",searchCount);

        System.out.println(appSearchTS);
        return "search/search_main";
    }

    //搜索查询更多
    @PostMapping("/searchPage")
    public ModelAndView searchPage(HttpServletRequest request,Model model){
        String sear = request.getParameter("sear");
        String page = request.getParameter("page");
        if(sear==null){
            return new ModelAndView();
        }
        //搜索前十条
        List<AppSearchT> appSearchTS = commentClientService.searchPage(Integer.valueOf(page), 10, sear);
        if(appSearchTS!=null){
            appSearchTS.forEach(item->{
                item.setClassifyT(zxClientService.bycid(item.getClassifyId()));
                item.setAppUserT(userClientService.byid(item.getUserId()));
            });
        }
        model.addAttribute("appSearchTS",appSearchTS);
        return new ModelAndView("search/search_newPage");
    }



    @PostMapping("/consult_solo/newCommAdd")
    @ResponseBody
    public R newCommAdd(HttpServletRequest request){
        String commId = request.getParameter("commId");
        String content = request.getParameter("content");
        //String no = request.getParameter("no");//咨询ID
        String userid = request.getParameter("userid");
        R r = new R();
        if(commId==null || content==null || userid==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("异常评论");
            return r;
        }
        AppCommentT appCommentT = new AppCommentT();
        appCommentT.setUserId(Integer.valueOf(userid));
        appCommentT.setCommentContent(content);
        appCommentT.setCoverUserId(Integer.valueOf(commId));
        appCommentT.setCommentType("zx");
        appCommentT.setCommentGrade("2");
        appCommentT.setCommentStatus("1");
        System.out.println("提交评论的评论");
        System.out.println(appCommentT);
        Boolean aBoolean = commentClientService.commAdd(appCommentT);
        if(aBoolean){
           r.setCode(201);
           r.setSuccess(aBoolean);
           r.setMessage("恭喜你评论成功");
           return r;
        }
        return R.error();
    }


    @PostMapping("/consult_solo/commAdd")
    @ResponseBody
    public R commAdd(HttpServletRequest request){
        String text = request.getParameter("text");
        String essayId = request.getParameter("essayId");//咨询ID
        String userId = request.getParameter("userId");
        R r = new R();
        if(text == null){
           r.setCode(301);
           r.setSuccess(false);
           r.setMessage("请输入评论内容");
           return r;
        }
        if(essayId==null){
            return R.error();
        }
        if(userId==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("登录失效请重新登录再评论");
            return r;
        }

        AppCommentT appCommentT = new AppCommentT();
        appCommentT.setUserId(Integer.valueOf(userId));
        appCommentT.setCommentId(Integer.valueOf(essayId));
        appCommentT.setCommentType("zx");
        appCommentT.setCommentContent(text);
        appCommentT.setCommentGrade("1");
        appCommentT.setCommentStatus("1");
        System.out.println("添加评论---------》");
        System.out.println(appCommentT);

        Boolean aBoolean = commentClientService.commAdd(appCommentT);
        if(aBoolean){
            zxClientService.essayCommAdd(Integer.valueOf(essayId));
            r.setCode(201);
            r.setSuccess(aBoolean);
            r.setMessage("评论成功");
            return r;
        }

        return R.error();
    }
}
